﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class PatientForm : Form
    {
        BindingList<Patient> patList = new BindingList<Patient>();
        public PatientForm()
        {
            InitializeComponent();
            patGrid.AutoGenerateColumns = false;
            SetDataToGrid();
        }

       void FillRow(int i)
        {
            patGrid.Rows.Add();
            Patient p = patList[i];
            patGrid.Rows[i].Cells[0].Value = p.patName;
            patGrid.Rows[i].Cells[1].Value = p.surname;
            patGrid.Rows[i].Cells[2].Value = p.patronymic;
            patGrid.Rows[i].Cells[3].Value = p.dob;
          //  patGrid.Rows[i].Cells[4].Value = p.patDiagnose;

        }


        void SetDataToGrid()
        {
            patList = new BindingList<Patient>(PatientDB.GetPatients());
            patGrid.DataSource = patList;
          }

        private void AddBtn_Click(object sender, EventArgs e)
        {
            Patient pat = new Patient();
            PatientDetailsForm patDet = new PatientDetailsForm(pat);
            
            
            if (patDet.ShowDialog() == DialogResult.OK)
            {
                PatientDB.SavePatientToDB(pat, patList.ToList());
                patList.Add(pat);
                FillRow(patList.Count - 1);
            }
        }

        private void PatientForm_Load(object sender, EventArgs e)
        {

        }

        private void DeleteBtn_Click(object sender, EventArgs e)
        {
            int index = patGrid.CurrentRow.Index;

            if (MessageBox.Show("Are you sure you want to delete patient?", "Delete patient", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                PatientDB.DeleteDPatient(index);
                patList.RemoveAt(index);
             patGrid.Rows.RemoveAt(index);
                 SetDataToGrid();
                MessageBox.Show("Patient deleted!");
            }
        }

        private void EditBtn_Click(object sender, EventArgs e)
        {
            int index = patGrid.CurrentRow.Index;
            Patient patEdit = patList[index];
            PatientDetailsForm patDet = new PatientDetailsForm(patEdit);
            if (patDet.ShowDialog() == DialogResult.OK)
            {
                PatientDB.EditedPatientToDB(patEdit);
                patList.RemoveAt(index);
                MessageBox.Show("Changes saved!");
                //  patGrid.Rows.RemoveAt(index);
                SetDataToGrid();
                           }
        }

        /*private void AddBtn_Click(object sender, EventArgs e)
        {
            Patient p = new Patient();
            PatientDetailForm pForm = new PatientDetailForm(p);

            if (pForm.ShowDialog() == DialogResult.OK)
            {
               / PatientDB.SavePatientToDB(p);
                patList.Add(p);
                FillRow(patList.Count - 1);
            }
        }*/
    }
}
